<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:p="http://primefaces.org/ui"
                template="../../template/tagedu.xhtml">

    <ui:define name="title">
        <title><h:outputText value="TAG - Cadastro Ator"/></title>
        <f:metadata>
            <f:viewParam name="personage" value="#{actorRegistration.personageID}"/>
        </f:metadata>
    </ui:define>

    <ui:define id="conteudo" name="content">

        <f:event type="preRenderComponent" listener="#{actorRegistration.initialize()}"/>        
        <h:outputText styleClass="personageName" value="Cadastro de #{actorRegistration.getPersonageName()}"/>

        <h:form id="actorRegistrationForm">
            <p:messages/>
            <p:tabView styleClass="filiationTabView" id="filiationTabView">                
                <p:tab title="Dados Principais">
                    <p:panel styleClass="mainInfoTab">
                        <h:panelGrid columns="2" styleClass="panelGrids">
                            <h:outputLabel for="name" value="Nome*:"/>
                            <p:inputText id="name" styleClass="name" value="#{actorRegistration.human.firstName}" 
                                         required="true" requiredMessage="'Nome' é obrigatório"/>

                            <h:outputLabel for="lastName" value="Sobrenome*:"/>
                            <p:inputText id="lastName" styleClass="name" value="#{actorRegistration.human.lastName}" 
                                         required="true" requiredMessage="'Sobrenome' é obrigatório"/>

                            <h:outputLabel for="sex" value="Sexo*:"/>
                            <h:selectOneMenu id="sex" value="#{actorRegistration.human.sex}">
                                <f:selectItems value="#{actorRegistration.sexList}"/>
                            </h:selectOneMenu>

                            <h:outputLabel for="birthday" value="Nascimento*:"/>
                            <p:inputMask id="birthday" mask="99/99/9999" required="true" 
                                         requiredMessage="'Nascimento' é obrigatório" value="#{actorRegistration.human.birthday.time}">
                                <f:convertDateTime pattern="dd/MM/yyyy"/>
                            </p:inputMask>

                            <h:outputLabel for="nacionality" value="Nacionalidade*:"/>
                            <h:selectOneMenu id="nacionality" value="#{actorRegistration.nationalityID}">
                                <f:selectItems value="#{actorRegistration.nationalityList}"/>
                            </h:selectOneMenu>

                            <h:outputLabel for="ethnicity" value="Etnia*:"/>
                            <h:selectOneMenu id="ethnicity" value="#{actorRegistration.ethnicityID}">
                                <f:selectItems value="#{actorRegistration.ethnicityList}"/>
                            </h:selectOneMenu>

                            <h:outputLabel for="phone" value="Telefone Fixo:"/>
                            <p:inputMask id="phone" mask="(99)9999-9999" value="#{actorRegistration.human.phone}"/>

                            <h:outputLabel for="celphone" value="Celular:"/>
                            <p:inputMask id="celphone" mask="(99)9999-9999" value="#{actorRegistration.human.celphone}"/>

                            <h:outputLabel for="email" value="Email:"/>
                            <p:inputText id="email" value="#{actorRegistration.human.email}"/>

                            <h:outputLabel for="laterality" value="Lateralidade*:"/>
                            <h:selectOneMenu id="laterality" value="#{actorRegistration.human.laterality}">
                                <f:selectItems value="#{actorRegistration.lateralityList}"/>
                            </h:selectOneMenu>

                            <h:outputLabel for="maritalStatus" value="Estado Civil*:"/>
                            <h:selectOneMenu id="maritalStatus" value="#{actorRegistration.human.maritalStatus}">
                                <f:selectItems value="#{actorRegistration.maritalStatusList}"/>
                            </h:selectOneMenu>

                        </h:panelGrid>
                    </p:panel>
                </p:tab>
                <p:tab title="Endereço">
                    <p:panel id="address" styleClass="addressPanel">
                        <p:message for="cep"/>
                        <h:outputLabel value="CEP*" for="cep"/>
                        <p:inputMask id="cep" value="#{actorRegistration.cep}" mask="99999999" required="true" requiredMessage="'CEP' é obrigatório"/>
                        <p:commandButton value="Buscar" ajax="true" action="#{actorRegistration.searchCEP()}" update="address" process="@this, cep"/>
                        <br/>
                        <h:panelGroup id="zipcode">
                            <h:outputLabel value="Estado*:" for="state"/>
                            <p:inputText id="state" value="#{actorRegistration.zipcode.city.state.name}" required="true" readonly="true"/>
                            <br/>
                            <h:outputLabel value="Cidade*:" for="city"/>
                            <p:inputText id="city" value="#{actorRegistration.zipcode.city.name}" required="true" readonly="true"/>
                            <br/><br/>
                        </h:panelGroup>
                        <h:panelGroup rendered="#{actorRegistration.showAddress}">
                            <h:outputLabel value="Bairro*:" for="neighborhood"/>
                            <p:autoComplete id="neighborhood" value="#{actorRegistration.neighborhood}" 
                                            completeMethod="#{actorRegistration.neighborhoodComplete}" 
                                            required="true" requiredMessage="'Bairro' é campo obrigatório"
                                            minQueryLength="3"/>
                            <br/>
                            <h:outputLabel value="Logradouro*:" for="street"/>
                            <p:autoComplete id="street" value="#{actorRegistration.street}" 
                                            completeMethod="#{actorRegistration.streetComplete}" 
                                            required="true" requiredMessage="'Logradouro' é campo obrigatório"
                                            minQueryLength="3"/>
                            <br/><br/>
                            <h:outputLabel value="Número:" for="number"/>
                            <p:inputText id="number" value="#{actorRegistration.address.number}"/>
                            <br/>
                            <h:outputLabel value="Complemento" for="complement"/>
                            <p:inputText id="complement" value="#{actorRegistration.address.complement}"/>                            
                        </h:panelGroup>
                    </p:panel>
                </p:tab>


                <p:tab title="Dados Específicos">
                    <p:panel>
                        <ui:repeat value="#{actorRegistration.additionalInfoList}" var="additionalInfo">
                            <h:panelGrid columns="2" styleClass="panelGrids">
                                <h:outputText styleClass="specificInfo" value="#{additionalInfo.additionalInfoType.acronym}:"/>
                                <p:inputMask id="addInfoMask" mask="#{additionalInfo.additionalInfoType.mask}" 
                                             value="#{additionalInfo.value}"
                                             required="#{additionalInfo.additionalInfoType.compulsory}" 
                                             requiredMessage="'#{additionalInfo.additionalInfoType.acronym}' é obrigatório"/>

                            </h:panelGrid>
                        </ui:repeat>
                    </p:panel>
                </p:tab>


                <p:tab title="Filiação" >
                    <h:panelGroup id="filiation">
                        <p:panel header="Pai" styleClass="humanFiliation">
                            <p:commandLink value="Buscar" ajax="true"
                                           process="@this"
                                           onstart="statusDialog.show();" 
                                           oncomplete="statusDialog.hide(); searchHuman.show();" 
                                           action="#{actorRegistration.prepareFatherSearch()}"
                                           update=":searchHumanDialog"/>
                            <p:commandLink value="Cadastrar" ajax="true"
                                           process="@this"
                                           onstart="statusDialog.show();" 
                                           oncomplete="statusDialog.hide(); addHuman.show();" 
                                           action="#{actorRegistration.prepareNewMale()}"
                                           update=":addHumanDialog"/>
                            <div class="clear"></div>
                            <br/>
                            <h:outputLabel value="Nome: "/>
                            <h:outputText rendered="#{actorRegistration.humanFather != null}" 
                                          value="#{actorRegistration.humanFather.name}"/>
                        </p:panel>
                        <p:panel header="Mãe" styleClass="humanFiliation">
                            <p:commandLink value="Buscar" ajax="true"
                                           process="@this"
                                           onstart="statusDialog.show();" 
                                           oncomplete="statusDialog.hide(); searchHuman.show();" 
                                           action="#{actorRegistration.prepareMotherSearch()}"
                                           update=":searchHumanDialog"/>
                            <p:commandLink value="Cadastrar" ajax="true"
                                           process="@this"
                                           onstart="statusDialog.show();" 
                                           oncomplete="statusDialog.hide(); addHuman.show();" 
                                           action="#{actorRegistration.prepareNewFemale()}"
                                           update=":addHumanDialog"/>
                            <div class="clear"></div>
                            <br/>
                            <h:outputLabel value="Nome: "/>
                            <h:outputText rendered="#{actorRegistration.humanMother != null}"
                                          value="#{actorRegistration.humanMother.name}"/>
                        </p:panel>
                        <p:panel header="Responsável" styleClass="humanFiliation">
                            <p:commandLink value="Buscar" ajax="true"
                                           process="@this"
                                           onstart="statusDialog.show();" 
                                           oncomplete="statusDialog.hide(); searchHuman.show();" 
                                           action="#{actorRegistration.prepareSponsorSearch()}"
                                           update=":searchHumanDialog"/>
                            <p:commandLink value="Cadastrar" ajax="true"
                                           process="@this"
                                           onstart="statusDialog.show();" 
                                           oncomplete="statusDialog.hide(); addHuman.show();" 
                                           action="#{actorRegistration.prepareNewHuman()}"
                                           update=":addHumanDialog"/>
                            <div class="clear"></div>
                            <br/>
                            <h:outputLabel value="Nome: "/>
                            <h:outputText rendered="#{actorRegistration.humanSponsor != null}"
                                          value="#{actorRegistration.humanSponsor.name}"/>
                        </p:panel>
                    </h:panelGroup>
                    <div class="clear"></div>
                </p:tab>


                <p:tab title="Registro" >
                    <p:panel>
                        <h:panelGrid columns="1" styleClass="panelGrids">
                            <h:panelGroup id="unitiesForRegister">
                                <h:outputLabel for="unity" value="Local*:"/>
                                <h:selectOneMenu id="unity" value="#{actorRegistration.unityForRegisterID}" 
                                                 required="true" requiredMessage="'Local' é obrigatório">
                                    <f:selectItems value="#{actorRegistration.unitiesForRegister}"/>                                    
                                </h:selectOneMenu>

                            </h:panelGroup>
                        </h:panelGrid>
                    </p:panel>
                </p:tab>
            </p:tabView>
            <div class="buttonGroup">
                <p:commandButton onstart="statusDialog.show();" onsuccess="statusDialog.hide();"
                                 onerror="statusDialog.hide();"
                                 value="Cadastrar" resetValues="true" process="@form" update="@form" action="#{actorRegistration.save()}"
                                 ajax="true"/>
            </div>
        </h:form>
    </ui:define>

    <ui:define name="modals">
        <p:dialog modal="true" widgetVar="statusDialog"
                  draggable="false" closable="false">
            <p:graphicImage value="../../resources/images/ajax/ajaxloader.gif"/>
        </p:dialog>


        <p:dialog id="searchHumanDialog" position="center" modal="true" 
                  widgetVar="searchHuman" closable="true" 
                  resizable="false" width="800" styleClass="noTitle">
            <h:form>
                <p:dataTable value="#{actorRegistration.humanList}" var="human" paginator="true" rows="10" lazy="true"
                             paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"  
                             rowsPerPageTemplate="10,25,50"
                             selection="#{actorRegistration.genericHuman}" rowKey="#{human.id}">
                    <p:column selectionMode="single" />
                    <p:column rendered="false">
                        <h:outputText value="#{human.id}"/>
                    </p:column>
                    <p:column>
                        <p:rowToggler/>
                    </p:column>

                    <p:column filterMatchMode="contains" filterStyle="width: 200px" 
                              filterBy="name">
                        <f:facet name="header">
                            <h:outputText value="Nome"/>
                        </f:facet>
                        <h:outputText value="#{human.name}"/>
                    </p:column>
                    <p:rowExpansion>
                        <h:panelGrid columns="2" cellpadding="4">
                            <h:outputLabel value="Nascimento: " for="birthday"/>
                            <h:outputText value="#{human.birthday.time}" id="birthday">
                                <f:convertDateTime pattern="dd/MM/yyyy"/>
                            </h:outputText>
                            <h:outputLabel value="Endereço: " for="address"/>
                            <h:outputText value="#{human.address.street.name}, #{humanaddress.number}.
                                          #{human.address.complement}. #{human.address.zipcode.city.name}/#{human.address.zipcode.city.state.name}" id="address"/>
                            <ui:repeat value="#{actorRegistration.personAdditionalInfo(human.id)}" var="addInfo">
                                <h:outputLabel value="#{addInfo.additionalInfoType.acronym}: "/>
                                <h:outputText value="#{addInfo.value}"/>
                            </ui:repeat>
                        </h:panelGrid>
                    </p:rowExpansion>
                </p:dataTable>
                <div class="buttonGroupModal">
                    <p:commandButton value="OK" onstart="statusDialog.show();" 
                                     oncomplete="statusDialog.hide(); searchHuman.hide();"
                                     ajax="true" process="@form" 
                                     action="#{actorRegistration.configureFatherMotherSponsor()}" 
                                     update=":actorRegistrationForm:filiationTabView:filiation"/>
                    <p:commandButton value="Cancelar" onclick="searchHuman.hide();"/>
                </div>
            </h:form>
        </p:dialog>


        <p:dialog id="addHumanDialog" position="center" modal="true"
                  widgetVar="addHuman" closable="true" resizable="true"
                  width="800" styleClass="noTitle">
            <h:form id="addHumanForm">
                <p:messages/>
                <p:tabView id="addHumanView">
                    <p:tab title="Dados Principais">
                        <p:panel styleClass="mainInfoTab">
                            <h:panelGrid columns="2">
                                <h:outputLabel for="humanName" value="Nome*:"/>
                                <p:inputText id="humanName" value="#{actorRegistration.humanRegister.firstName}"
                                             required="true" requiredMessage="'Nome' é obrigatório"/>
                                <h:outputLabel for="humanLastName" value="Sobrenome*:"/>
                                <p:inputText id="humanLastName" value="#{actorRegistration.humanRegister.lastName}"
                                             required="true" requiredMessage="'Sobrenome' é obrigatório"/>
                                <h:outputLabel for="humanSex" value="Sexo*:"/>
                                <h:selectOneMenu id="humanSex" value="#{actorRegistration.humanRegister.sex}">
                                    <f:selectItems value="#{actorRegistration.sexListRegister}"/>
                                </h:selectOneMenu>
                                <h:outputLabel for="humanBirthday" value="Nascimento*:"/>
                                <p:inputMask id="humanBirthday" mask="99/99/9999" value="#{actorRegistration.humanRegister.birthday.time}"
                                             required="true" requiredMessage="'Nascimento' é obrigatório">
                                    <f:convertDateTime pattern="dd/MM/yyyy"/>
                                </p:inputMask>
                                <h:outputLabel for="humanNacionality" value="Nacionalidade:"/>
                                <h:selectOneMenu id="humanNacionality" value="#{actorRegistration.nationalityIDRegister}">
                                    <f:selectItems value="#{actorRegistration.nationalityList}"/>
                                </h:selectOneMenu>
                                <h:outputLabel for="humanEthnicity" value="Etnia:"/>
                                <h:selectOneMenu id="humanEthnicity" value="#{actorRegistration.ethnicityIDRegister}">
                                    <f:selectItems value="#{actorRegistration.ethnicityList}"/>
                                </h:selectOneMenu>
                                <h:outputLabel for="humanPhone" value="Telefone Fixo:"/>
                                <p:inputMask id="humanPhone" mask="(99)9999-9999" value="#{actorRegistration.humanRegister.phone}"/>
                                <h:outputLabel for="humanCelphone" value="Celular:"/>
                                <p:inputMask id="humanCelphone" mask="(99)9999-9999" value="#{actorRegistration.humanRegister.celphone}"/>
                                <h:outputLabel for="humanEmail" value="Email:"/>
                                <p:inputText id="humanEmail" value="#{actorRegistration.humanRegister.email}"/>
                                <h:outputLabel for="humanLaterality" value="Lateralidade:"/>
                                <h:selectOneMenu id="humanLaterality" value="#{actorRegistration.humanRegister.laterality}">
                                    <f:selectItems value="#{actorRegistration.lateralityList}"/>
                                </h:selectOneMenu>
                                <h:outputLabel for="humanMaritalStatus" value="Estado Civil:"/>
                                <h:selectOneMenu id="humanMaritalStatus" value="#{actorRegistration.humanRegister.maritalStatus}">
                                    <f:selectItems value="#{actorRegistration.maritalStatusList}"/>
                                </h:selectOneMenu>                
                            </h:panelGrid>
                        </p:panel>
                    </p:tab>
                    <p:tab title="Endereço">
                        <p:panel id="address" styleClass="addressPanel">
                            <p:message for="cep"/>
                            <h:outputLabel value="CEP*" for="cep"/>
                            <p:inputMask id="cep" value="#{actorRegistration.cepRegister}" mask="99999999" required="true" requiredMessage="'CEP' é obrigatório"/>
                            <h:commandButton value="Buscar">
                                <f:ajax execute="cep" render="address" listener="#{actorRegistration.searchCEPRegister()}"/>
                            </h:commandButton>
                            <br/>
                            <h:panelGroup id="zipcode">
                                <h:outputLabel value="Estado*:" for="state"/>
                                <p:inputText id="state" value="#{actorRegistration.zipcodeRegister.city.state.name}" required="true" readonly="true"/>
                                <br/>
                                <h:outputLabel value="Cidade*:" for="city"/>
                                <p:inputText id="city" value="#{actorRegistration.zipcodeRegister.city.name}" required="true" readonly="true"/>
                                <br/><br/>
                            </h:panelGroup>
                            <h:panelGroup rendered="#{actorRegistration.showAddressRegister}">
                                <h:outputLabel value="Bairro*:" for="neighborhood"/>
                                <p:autoComplete id="neighborhood" value="#{actorRegistration.neighborhoodRegister}" 
                                                completeMethod="#{actorRegistration.neighborhoodCompleteRegister}" 
                                                required="true" requiredMessage="'Bairro' é campo obrigatório"
                                                minQueryLength="3"/>
                                <br/>
                                <h:outputLabel value="Logradouro*:" for="street"/>
                                <p:autoComplete id="street" value="#{actorRegistration.streetRegister}" 
                                                completeMethod="#{actorRegistration.streetCompleteRegister}" 
                                                required="true" requiredMessage="'Logradouro' é campo obrigatório"
                                                minQueryLength="3"/>
                                <br/><br/>
                                <h:outputLabel value="Número:" for="number"/>
                                <p:inputText id="number" value="#{actorRegistration.addressRegister.number}"/>
                                <br/>
                                <h:outputLabel value="Complemento" for="complement"/>
                                <p:inputText id="complement" value="#{actorRegistration.addressRegister.complement}"/>                            
                            </h:panelGroup>
                        </p:panel>
                    </p:tab>    
                    <p:tab title="Dados Específicos">
                        <p:panel>
                            <h:panelGrid columns="2" id="addInfoPanel">
                                <h:selectOneMenu id="addInfoTypes" value="#{actorRegistration.addInfoTypeIDRegister}">
                                    <f:selectItems value="#{actorRegistration.additionalInfoTypeListRegister}"/>
                                    <f:ajax execute="addInfoTypes" render="addInfoPanel" listener="#{actorRegistration.loadMask(actorRegistration.addInfoTypeIDRegister)}"/>
                                </h:selectOneMenu>
                                <p:inputMask mask="#{actorRegistration.mask}" value="#{actorRegistration.additionalInfoRegister}"/>
                            </h:panelGrid>
                        </p:panel>
                    </p:tab>                 
                </p:tabView>
                <div class="buttonGroupModal">
                    <p:commandButton value="OK" onstart="statusDialog.show();"
                                     oncomplete="statusDialog.hide(); handleSubmitRequest(xhr, status, args)"
                                     ajax="true" process="@form" 
                                     action="#{actorRegistration.createNewHuman()}" 
                                     update=":actorRegistrationForm:filiationTabView:filiation @form"/>
                    <p:commandButton value="Cancelar" onclick="addHuman.hide();"/>
                </div>
            </h:form> 
        </p:dialog>

        <script type="text/javascript">
            function handleSubmitRequest(xhr, status, args) {
                if (!args.validationFailed) {
                    addHuman.hide();
                }
            }
        </script>
    </ui:define>
</ui:composition>